package com.hao.tool.web.module.navigation.controller;

import com.hao.tool.common.enums.UseTypeEnum;
import com.hao.tool.web.common.constant.Commons;
import com.hao.tool.web.common.constant.LayPage;
import com.hao.tool.web.common.constant.LayResponse;
import com.hao.tool.web.module.navigation.dto.NavigationDTO;
import com.hao.tool.web.module.navigation.entity.ModelTypeInfo;
import com.hao.tool.web.module.navigation.entity.NavigationInfo;
import com.hao.tool.web.module.navigation.service.ModelTypeService;
import com.hao.tool.web.module.navigation.service.NavigationService;
import com.hao.tool.web.module.rbac.entity.ManagerInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 导航栏
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-21 21:47
 */
@RestController
@RequestMapping("/rest/navigation")
public class NavigationRestController {
    @Autowired
    private NavigationService navigationService;
    @Autowired
    private ModelTypeService modelTypeService;


    @RequestMapping("/list/{modelIds}")
    public LayResponse list(HttpServletRequest request, @PathVariable String modelIds) {
        String userName = (String) request.getAttribute(Commons.SUB_KEY);
        List<ModelTypeInfo> modelTypeInfos = modelTypeService.find(UseTypeEnum.SHOW.getCode(), userName);
        List<ModelTypeInfo> filterModelTypeInfos = new ArrayList<>();

        if (StringUtils.isNotBlank(modelIds)
                && modelIds.contains("d")) {
            for (String d : modelIds.split("d")) {
                for (ModelTypeInfo modelTypeInfo : modelTypeInfos) {
                    if (String.valueOf(modelTypeInfo.getId()).equals(d)) {
                        filterModelTypeInfos.add(modelTypeInfo);
                    }
                }
            }
        } else {
            filterModelTypeInfos.addAll(modelTypeInfos);
        }
        List<ModelTypeInfo> existModelTypeInfos = new ArrayList<>();
        filterModelTypeInfos.forEach(modelTypeInfo -> {
            List<NavigationInfo> navigationInfoList = navigationService.findByModelId(UseTypeEnum.SHOW.getCode(), modelTypeInfo.getId(), userName);
            if (navigationInfoList != null
                    && !navigationInfoList.isEmpty()) {
                modelTypeInfo.setInfoList(navigationInfoList);
                existModelTypeInfos.add(modelTypeInfo);
            }
        });
        LayResponse layResponse = new LayResponse<>();
        layResponse.setCode(LayResponse.SUCCESS);
        layResponse.setData(existModelTypeInfos);
        if (StringUtils.isNotBlank(userName)) {
            layResponse.setStatus(1);
            ManagerInfo managerInfo = (ManagerInfo) request.getAttribute(Commons.MANAGER);
            layResponse.setAuth(managerInfo);
        } else {
            layResponse.setStatus(0);
        }
        return layResponse;
    }

    @RequestMapping("/list")
    public LayResponse list(HttpServletRequest request) {
        String userName = (String) request.getAttribute(Commons.SUB_KEY);
        List<ModelTypeInfo> modelTypeInfos = modelTypeService.find(UseTypeEnum.SHOW.getCode(), userName);
        List<ModelTypeInfo> existModelTypeInfos = new ArrayList<>();
        modelTypeInfos.forEach(modelTypeInfo -> {
            List<NavigationInfo> navigationInfoList = navigationService.findByModelId(UseTypeEnum.SHOW.getCode(), modelTypeInfo.getId(), userName);
            if (navigationInfoList != null
                    && !navigationInfoList.isEmpty()) {
                modelTypeInfo.setInfoList(navigationInfoList);
                existModelTypeInfos.add(modelTypeInfo);
            }
        });
        LayResponse layResponse = new LayResponse<>();
        layResponse.setCode(LayResponse.SUCCESS);
        layResponse.setData(existModelTypeInfos);
        if (StringUtils.isNotBlank(userName)) {
            layResponse.setStatus(1);
            ManagerInfo managerInfo = (ManagerInfo) request.getAttribute(Commons.MANAGER);
            layResponse.setAuth(managerInfo);
        } else {
            layResponse.setStatus(0);
        }
        return layResponse;
    }

    @RequestMapping("/listTable")
    public LayPage listTable(Long modelId, String linkName,
                             LayPage<NavigationInfo> page) {
        LayPage<NavigationInfo> layPage = (LayPage<NavigationInfo>) navigationService.page(page);
        return layPage;
    }

    @PostMapping("/add")
    public LayResponse add(NavigationDTO dto, String userName, HttpServletRequest request) {
        userName = (String) request.getAttribute(Commons.USER_NAME);
        NavigationInfo info = navigationService.addNavigation(dto, userName);
        LayResponse layResponse = new LayResponse<>();
        layResponse.setCode(LayResponse.SUCCESS)
                .setData(info);
        return layResponse;
    }

    @RequestMapping("/addRecord")
    public LayResponse addRecord(Long linkId) {
        navigationService.incrementClickCount(linkId);
        LayResponse layResponse = new LayResponse<>();
        layResponse.setCode(LayResponse.SUCCESS);
        return layResponse;
    }
}
